package com.lt.flashcard.view;

import java.io.IOException;
import java.util.List;

import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Path;
import org.zkoss.zk.ui.util.Clients;

import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Flashcard;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.FlashcardService;
import com.lt.flashcard.model.service.Repository;

public class V_flashcard_list {
    private List<Flashcard> fcList;
    private User user = Utility.getUser();
    
    public void setFcList(List<Flashcard> fcList){
    	this.fcList = fcList;
    }

    public List<Flashcard> getFcList() {
    	Clients.evalJavaScript("$.updateSuggestionList(" + (fcList != null ? fcList.size() : 0) + ");");
    	return fcList;
    }
    
    /**
     * When user typing on the term inputbox, receive from flashcardctrl
     * @param strTerm
     * @param flashcardset
     */
    @GlobalCommand
    @NotifyChange({"fcList"})
    public void doChange(@BindingParam("term") String strTerm, @BindingParam("flashcardset") FlashcardSet flashcardset) {
    	if(strTerm.length() >= 3){
        	FlashcardService fcSrv = Repository.getInstance().getFlashcardService();
        	fcList = fcSrv.getAllowedFlashcard(user, flashcardset, strTerm);
    	}else{
    		fcList = null;
    	}
    }
    
    @Command
    public void insertExitstFlashcardToFlashcard(@BindingParam("flashcard") Flashcard eFlashcard) throws IOException{
		Component com = Path.getComponent("//page_flashcardlist/flashcardlist_parent");
		FlashcardCtrl ctrl = (FlashcardCtrl) com.getAttribute("$composer");
		//set view flashcard
		updateFlashcard(ctrl, eFlashcard);
		//set new flashcard
		Flashcard fc = createFlashcard(eFlashcard);
		ctrl.setFlashcard(fc);
		ctrl.soundBack.setValue(eFlashcard.getSoundBack());
		ctrl.soundFront.setValue(eFlashcard.getSoundFront());
    }
    
    private void updateFlashcard(FlashcardCtrl ctrl, Flashcard eFlashcard) throws IOException{
		ctrl.fDefinition.setValue(eFlashcard.getDefinition());
		ctrl.fNotes.setValue(eFlashcard.getNotes());
		ctrl.fImage.setSrc(Utility.loadImageFromGridFS(eFlashcard.getImage(), ""));
		ctrl.fTerm.setValue(eFlashcard.getTerm());
		if(eFlashcard.getImage().length() > 0)
			Clients.evalJavaScript("$('.tdimage').show();"
					+ "$('.fdefinition').css({'width': '210px', 'margin-left': '0px'});");
    }
    
    private Flashcard createFlashcard(Flashcard eFlashcard){
		Flashcard fc = new Flashcard();
		fc.setDefinition(eFlashcard.getDefinition());
		fc.setFlashcardSet(eFlashcard.getFlashcardSet());
		fc.setImage(eFlashcard.getImage());
		fc.setNotes(eFlashcard.getNotes());
		fc.setSoundFront(eFlashcard.getSoundFront());
		fc.setSoundBack(eFlashcard.getSoundBack());
		fc.setTerm(eFlashcard.getTerm());
		fc.setStatus(eFlashcard.getStatus());
		return fc;
    }
}
